<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

abstract class Sort extends SourceData
{

    public $moveCount;
    public $compareCount;
    public $name;

    function __construct($SourceData)
    {
        $this->data = $SourceData->data;
        $this->groupNumber = $SourceData->groupNumber;
        $this->testNumber = $SourceData->testNumber;
        $this->movecount = array();
        $this->compareCount = array();
    }

    function sort($index)
    {
        
    }

    function begin()
    {
        for ($index = 0; $index < $this->groupNumber; $index++)
        {

            $this->moveCount[$index] = 0;
            $this->compareCount[$index] = 0;
            $this->Sort($index);
        }
        $this->format();
    }

    function beginC()
    {
        for ($i = 0; $i < $this->groupNumber; $i++)
        {
            $command = './' . $this . " " . implode(" ", $this->data[$i]);
//            var_dump($command);
            $return = exec($command);
//            var_dump($return);
            $return = explode("/", $return);
            $this->moveCount[$i] = $return[0];
            $this->compareCount[$i] = $return[1];
            $this->printData[$i] = $return[2];
            $this->printData[$i] = str_ireplace("n", "\n", $this->printData[$i]);
        }
    }

    function beginC2()
    {
        for ($i = 0; $i < $this->groupNumber; $i++)
        {
            $command = './' ."SortC " .$this . " " . implode(" ", $this->data[$i]);
//            var_dump($command);
//            echo "<br>";
            $return = exec($command);
//            var_dump($return);
            $return = explode("/", $return);
            $this->moveCount[$i] = $return[0];
            $this->compareCount[$i] = $return[1];
            $this->printData[$i] = $return[2];
            $this->printData[$i] = str_ireplace("n", "\n", $this->printData[$i]);
        }
    }

    function printCount()
    {
        echo "<tr><td>$this->name</td>";
        for ($index = 0; $index < $this->groupNumber; $index++)
        {
            echo "<td>" . $this->moveCount[$index] . "</td>";
            echo "<td>" . $this->compareCount[$index] . "</td>";
        }
//        echo "<td><form action='sss.php' method='get' id='bubble'>
//              <input type='hidden' name='country' value='bubble' />
//
//";
//          echo " <button id='result' type='submit' form='nameform' >查看排序结果</button></form></td>";

        echo "<td><a id = 'result' href = #" . $this . "ShowResult target='_top'>查看排序结果</a></td>";
        echo "</tr>";
    }

    public function format()
    {
        for ($i = 0; $i < $this->groupNumber; $i++)
        {
//                $this->printData[$i] = ""; 
            for ($j = 0; $j < $this->testNumber; $j++)
            {
                $this->printData[$i] .= $this->data[$i][$j] . "\t";
                if (0 == ($j + 1) % 6)
                {
                    $this->printData[$i].= "\n";
                }
            }
//            var_dump($this->printData[$i]);
        }
    }

}
